package com.yungu.swift.admin.route.operation;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.base.CurrentUser;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.admin.AdminStatusParam;
import com.yungu.swift.base.model.admin.AdminUuidParam;
import com.yungu.swift.system.sys.model.dto.SysCompanyDto;
import com.yungu.swift.system.sys.model.param.AdminCompanyPageParam;
import com.yungu.swift.system.sys.model.param.AdminCompanyParam;
import com.yungu.swift.system.sys.model.vo.CompanyDetailVo;
import com.yungu.swift.system.sys.model.vo.CompanyVo;
import com.yungu.swift.system.sys.service.SysCompanyService;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author : cuixiuyin
 * @date : 2019/6/20
 */
@RestController
@RequestMapping("/admin/v1/operation/company")
@Api(value = "【运营管理】", tags = "企业管理")
public class CompanyAdmin {

    @Reference
    private SysCompanyService sysCompanyService;

    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "企业信息分页查询", httpMethod = "POST")
    public ResponseData<PageVo<CompanyVo>> queryPage(@RequestBody AdminCompanyPageParam companyPageParam) {
        return sysCompanyService.queryPageVo(companyPageParam);
    }

    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "企业信息新增或更新", httpMethod = "POST")
    public ResponseData<Boolean> saveOrUpdate(@RequestBody AdminCompanyParam companyParam) {
        SysCompanyDto sysCompanyDto = new SysCompanyDto();
        BeanUtils.copyProperties(companyParam, sysCompanyDto);
        if (StringUtils.isEmpty(sysCompanyDto.getUuid())) {
            sysCompanyDto.setUuid(StringUtils.buildUUID());
            sysCompanyDto.setCreateOn(new Date());
            sysCompanyDto.setCreateBy(companyParam.getUserUuid());
            sysCompanyDto.setAppid(companyParam.getAppid());
            return sysCompanyService.add(sysCompanyDto);
        }
        sysCompanyDto.setUpdateOn(new Date());
        sysCompanyDto.setUpdateBy(companyParam.getUserUuid());
        return sysCompanyService.edit(sysCompanyDto);
    }

    @RequestMapping(value = "/changeStatus", method = RequestMethod.POST)
    @ApiOperation(value = "企业信息开通或关闭", httpMethod = "POST")
    public ResponseData<Boolean> changeStatus(@RequestBody AdminStatusParam companyStatusParam) {
        if (ParamUtil.isIllegal(companyStatusParam.getUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        SysCompanyDto companyDto = new SysCompanyDto();
        BeanUtils.copyProperties(companyStatusParam, companyDto);
        companyDto.setUpdateBy(companyStatusParam.getUserUuid());
        return sysCompanyService.edit(companyDto);
    }


    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "企业信息详情", httpMethod = "GET")
    public ResponseData<CompanyDetailVo> detail(@RequestParam String uuid, @CurrentUser AdminUuidParam uuidParam) {
        if (ParamUtil.isIllegal(uuid)) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return sysCompanyService.getDetail(uuid, uuidParam.getAppid());
    }

}
